<?php

namespace App\Http\Controllers\Customer;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Http\Query\GoodsWearQuery;

class GoodsWearController extends Controller
{

    /**
     * 在庫照会wearの場合
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {

        $goodsId = $request->input('goodsId', 56);

        // 商品取得
        $goodsDetailQuery = new GoodsWearQuery;
        $goodsDetail = $goodsDetailQuery->getGoodsDetail($goodsId);

        // 商品画像取得
        $goodsImages = $goodsDetailQuery->getGoodsImage($goodsId);
        $goodsImage = $goodsImages[0]->image_url . $goodsImages[0]->file_name;

        $houseList = DB::table('goods_model as gm')
            ->leftJoin('storehouse_detailed_size as sd', 'gm.goods_id', '=', DB::raw("sd.goods_id and gm.goods_model_id=sd.model_id"))
            ->where(['gm.goods_id' => $goodsId])
            ->select(DB::raw('gm.*,sd.num'))->get();

        $data = [];
        if ($houseList != false) {
            foreach ($houseList as $key=>$val) {
                $val->size_name = fun_size($val->size);
                $val->color_name = fun_colors($val->color);
                if($val->num >= 51){
                    $val->style = '◎';
                }elseif ($val->num <= 50 && $val->num >=16){
                    $val->style = '○';
                }elseif($val->num == null){
                    $val->style = '入荷予定';
                }else{
                    $val->style = $val->num;
                }
                $data[$val->color][] = $val;
            }
        }
        $houseList = $data;
        return view('customer/goods_stock_wear', compact('goodsId', 'goodsDetail', 'goodsImage', 'houseList'));
    }


}